import { http } from '../../utils/net.js'
import { authChannel } from '../../utils/util.js'
import { inputgetName, normalInput } from '../../utils/inputBothBind.js'
Page({
  data: {
    cardInfo:null,
    money:"",
    dates:[],
    times:0,
    maxTimes:0,
    minTimes:0,
    remainMoney:0.00,
    poundage:0,
    timesFee:0,
    selectDates: '',
    nowTime:'',
    channelId: null,
    minDate: new Date().toLocaleDateString(),
    maxDate:null,
    displayMM: new Date().toLocaleDateString(),
    timeout: null,
    smsCode:""
  },
  inputgetName: inputgetName,
  normalInput: normalInput,
  changeMoney(e) {
    this.inputgetName(e)
    clearTimeout(this.data.timeout)
    this.setData({
      timeout: setTimeout(() => {
        this.getDefaultDates()
      }, 500)
    })  
  },
  
  toSelectDates:function() {
    this.selectComponent("#dates").open()
    this.selectComponent("#dates").initData()
  },
  confirmSelectDates(e) {
    this.selectComponent("#dates").close()
    if(e.detail.length) {
      this.setData({
        dates: e.detail,
        selectDates: `已选择了${e.detail.length}天`,
        times: e.detail.length + 1
      })
    } else {
      this.setData({
        dates: e.detail,
        selectDates: ""
      })
    }
    this.getMoneyHint(true)
  },
  createPlan:function() {
    http({
      url: '/api/index/index?method=make_refund_plan',
      data: {
        channel_id: this.data.channelId,
        bc_id: this.data.cardInfo.bc_id,
        times: this.data.times,
        dates: this.data.dates.join(","),
        pay_money: this.data.money,
        had_money: this.data.remainMoney,
        now_time: this.data.nowTime
      },
      isShowLoading: true,
      successCb: (res) => {
        console.log(res)
        wx.navigateTo({
          url: "/pages/refundConfirm/index",
          success:function(res1) {
            res1.eventChannel.emit('acceptDataFromOpenerPage', {
              data: res.data
            })
          }
        })
      }
    })
  },
  toCreatePlan() {
    this.createPlan()

    return

    authChannel({ channelId: this.data.channelId, cardInfo: this.data.cardInfo }).then(res => {
      console.log("page", res)
      if (res.code == 0 || res.code == -1) {
        this.createPlan()
      } else if (res.code == 1) {
        this.setData({
          orderNum: res.data
        })
        this.selectComponent("#auth").open()

      } else if (res.code == 2) {
        wx.navigateTo({
          url: "/pages/web/index",
          success: (res1) => {
            res1.eventChannel.emit('acceptDataFromOpenerPage', {
              url: res.data,
              callback: () => {
                this.createPlan()
              }
            })
          },
        })
      } else if (res.code == 3) {
        wx.navigateTo({
          url: "/pages/web/index",
          success: (res1) => {
            res1.eventChannel.emit('acceptDataFromOpenerPage', {
              url: res.data,
              callback: () => {
                this.createPlan()
                http({
                  url: "/api/index/index?method=del_html",
                  data: {
                    path: res.path
                  },
                  successCb: (res2) => { },
                  failCb: (err2) => { }
                })
              }
            })
          },
        })
      }
    })
  },
  toSendAuth2() {
    if (this.data.smsCode) {
      this.selectComponent("#auth").close()
      http({
        url: "/api/index/index?method=confirm_channel_authentication",
        data: {
          order_num: this.data.orderNum,
          sms_code: this.data.smsCode
        },
        isShowLoading:true,
        successCb: (res) => {
          this.createPlan()
        },
        failCb: (err) => {
          wx.showModal({
            title: "鉴权失败",
            content: err.msg,
            showCancel: false,
            confirmText: '我知道了',
            success: (res) => {
              if (res.confirm) {
                // TODO
                console.log(res)
              }
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '请填写验证码',
        icon: 'none'
      })
    }
  },
  toCancelAuth2() {
    this.selectComponent("#auth").close()
  },
  getDefaultDates() {
    http({
      url: "/api/index/index?method=get_mo_times",
      data: {
        bc_id: this.data.cardInfo.bc_id,
        pay_money: this.data.money
      },
      successCb:(res) => {
        this.setData({
          times: res.data.mo_times,
          nowTime: res.data.now_time,
          dates: res.data.dates,
          selectDates: `已选择了${res.data.dates.length}天`
        }) 
        this.getMoneyHint(true)
      }
    })
  },
  getMoneyHint(changeTimes) {
    let that = this
    http({
      url: "/api/index/index?method=start_money_hint",
      data: {
        channel_id:this.data.cardInfo.channel_id,
        bc_id:this.data.cardInfo.bc_id,
        times: this.data.times,
        dates: this.data.dates.join(","),
        pay_money: this.data.money
      },
      successCb: function(res) {
        if(changeTimes) {
          that.setData({
            times: res.data.mo_times
          })
        }
        that.setData({
          minTimes: res.data.min_times,
          maxTimes: res.data.max_times,
          channelId: res.data.channel_id,
          poundage: res.data.poundage,
          timesFee: res.data.times_money,
          remainMoney: res.data.had_money
        })
      },
      failCb: (err) => {
        this.setData({
          remainMoney: err.msg
        })
      }
    })
  },
  inTimes() {
    if(this.data.times >= this.data.maxTimes) {
      return
    }
    this.setData({
      times: this.data.times + 1
    })
    this.getMoneyHint(false)
  },
  deTimes() {
    if(this.data.times <= this.data.minTimes) {
      return
    }
    this.setData({
      times: this.data.times - 1
    })
    this.getMoneyHint(false)
  },
 
  onLoad: function (options) {
    let that = this
    let weekDays = [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31]
    let displayYY = new Date().getFullYear()
    let displayMM = new Date().getMonth() + 1
    if (displayYY % 4 == 0 && displayYY % 100 != 0 || displayYY % 400 == 0) {
      weekDays[1] = 29
    } else {
      weekDays[1] = 28
    }
    let tmp = displayMM == 12 ? `${displayYY + 1}/01/31` : `${displayYY}/${displayMM + 1}/${weekDays[displayMM]}`

    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      that.setData({
        cardInfo:data.cardInfo,
        maxDate:tmp
      })
    })
  }
})